<template>
  <a-popover v-model:visible="visible" trigger="click">
    <template #content>
      <div class="ex-color-picker" ref="el"></div>
        <div style="width: 100%;text-align: right">
            <a-space align="end">
                <a-button size="small" @click="clear">清空</a-button>
                <a-button size="small" @click="ok">确定</a-button>
            </a-space>
        </div>

    </template>
    <div class="color-picker">
      <div class="color-picker__trigger">
            <span class="color-picker__color">
                <span class="color-picker__color-inner" :style="{backgroundColor: value}">
                    <down-outlined class="color-picker__icon" v-if="value" />
                    <close-outlined class="el-color-picker__empty" v-else/>
                </span>

            </span>
      </div>
    </div>
  </a-popover>
</template>

<script>
    export default {
        name: "ExColorPicker"
    }
</script>
<script setup>
    import {ref,watch,nextTick,onBeforeUnmount} from "vue";
    import * as AColorPicker from "a-color-picker"
    const props = defineProps({
        value:String,
    })
    const emit = defineEmits(['update:value'])
    const el = ref()
    let color = props.value
    let selectColor = color
    const visible = ref(false)
    let picker = null
    let init  = false
    watch(visible,value=>{
      if(value){
        open()
      }else{
         emit('update:value',color)
      }
    })
    onBeforeUnmount(()=>{
        if(picker){
            picker = null
        }
    })
    function open(){
      if(!init){
          nextTick(()=>{
              setTimeout(()=>{
                  picker = AColorPicker.createPicker(el.value,{
                      color:props.value,
                      palette:['#f44336', '#e91e63', '#9c27b0', '#673ab7', '#3f51b5', '#2196f3', '#03a9f4', '#00bcd4', '#009688', '#4caf50', '#8bc34a', '#cddc39', '#ffeb3b', '#ffc107', '#ff9800', '#ff5722', '#795548', '#9e9e9e', '#607d8b','#000000'],
                  })
                  picker.on('change',function (e) {
                      selectColor = e.color
                  })
                  init = true
              },50)
          })
      }else{
          picker.setColor(props.value)
      }
    }
    function clear() {
        visible.value = false
        color = ''
        emit('update:value',color)
    }
    function ok() {
        color = selectColor
        visible.value = false
    }
</script>
<style scoped lang="scss">
.picker{
    position: fixed;
    z-index: 1000;
}
.color-picker{
    display: inline-block;
    position: relative;
    line-height: normal;
    height: 30px
}
.color-picker__trigger {
    display: inline-block;
    box-sizing: border-box;
    height: 30px;
    width: 30px;
    padding: 4px;
    border: 1px solid #e6e6e6;
    border-radius: 4px;
    position: relative;
    cursor: pointer;
}
.color-picker__color {
    position: relative;
    display: block;
    box-sizing: border-box;
    border: 1px solid #999;
    border-radius: 2px;
    width: 100%;
    height: 100%;
    text-align: center;
}
.color-picker__color-inner {
    position: absolute;
    left: 0;
    top: 0;
    right: 0;
    bottom: 0;
}
.color-picker__icon {
    font-size: 12px;
    display: inline-block;
    width: 100%;
    color: #fff;
    text-align: center;
}
.el-color-picker__empty{
    font-size: 12px;
    display: inline-block;
    width: 100%;
    text-align: center;
}
.dark-theme{
    .color-picker__trigger {
        border: 1px dashed #434343;
    }
}
.ex-color-picker :deep(.a-color-picker){
 box-shadow: none;
}
</style>
